package com.ag.lwc.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbCurrentEvent;
import com.ag.bean.TbEvent;
import com.ag.bean.TbEventCleanup;
import com.ag.bean.TbLog;
import com.ag.bean.TbLogPlanStep;
import com.ag.bean.TbLogType;
import com.ag.bean.TbPsContent;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.lwc.service.TbLogPsContentService;
import com.ag.lwc.service.TbLogTypeService;
import com.ag.lwc.service.impl.LogService;
import com.ag.processing.ProcessingPlans;
import com.ag.zrt.dao.TbCurrentEventDAO;
import com.ag.zrt.service.ILogPlanStepService;

//查询某个预案环节的具体内容,返回步骤名和步骤描述
//@Controller("currentPsContentAction")
//@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CurrentPsContentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -800116075966959572L;
	public ILogPlanStepService service;
	public String message;
	public String psId; // 当前环节的id
	public TbLogTypeService logTypeService;
	public LogService logService;
	// private ProcessingPlans st;
	private List<TbPsContent> currentContents; // 当前环节的内容
	// @Resource(name="ZRTTbCurrentEventDAO")
	private TbCurrentEventDAO currentEventDAO;
	private TbLogPsContentService psContentService;
	private List<TbLog> logList = null; // 当前要返回到页面的日志列表
	private static String newlogid = "10000000000000000000000000000000";// 初始化的日志id
	private String maxTime;
	private String eventId = null;

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getMaxTime() {
		return maxTime;
	}

	public void setMaxTime(String maxTime) {
		this.maxTime = maxTime;
	}

	public static String getNewlogid() {
		return newlogid;
	}

	public static void setNewlogid(String newlogid) {
		CurrentPsContentAction.newlogid = newlogid;
	}

	private String afterLogId; // 页面要传过来的日志id
	private List<TbLogType> logTypeList; // 日志类型列表

	public String getCurrentPsId() // 获取当前步骤id
	{
		/*
		 * String eventId = (String)
		 * this.getServletContext().getAttribute(EventUtil.PROCESSING_EVENT_ID);
		 * Session session;
		 * 
		 * session = currentEventDAO.getSessionFactory().openSession();
		 * 
		 * 
		 * if(eventId==null) { List<TbCurrentEvent> list =
		 * session.createSQLQuery
		 * ("select * from TB_CURRENT_EVENT").addEntity(TbCurrentEvent
		 * .class).list(); System.out.println(list.size()); TbCurrentEvent
		 * currentevent=list.get(0); //
		 * System.out.println("万才的action：CurrentPsContentAction: 步骤id："
		 * +currentevent.getEId()); psId = currentevent.getCurStepId();
		 * //当前步骤获取id
		 * 
		 * } else { TbCurrentEvent currentevent = (TbCurrentEvent)
		 * session.get(TbCurrentEvent.class, eventId); psId =
		 * currentevent.getCurStepId();
		 * 
		 * } session.close(); if(psId==null) {System.out.println("出错啦，没有步骤id");}
		 */
		if (eventId == null) {
			eventId = (String) getSession().getAttribute(
					EventUtil.PROCESSING_EVENT_ID);
		}
		System.out
				.println(eventId
						+ "**********************获取当前步骤id*******************************************");

		psId = currentEventDAO.findById(eventId).getCurStepId(); // 获取当前步骤id
		return psId;
	}

	public String getUpdateLogId() // 返回给页面已经查出来的最大日志id
	{
		System.out.println("万才的action： \t 返回给页面已经查出来的最大日志id: " + this.newlogid);
		message = newlogid;
		return "updateLogId";
	}

	public String getContents() // 查询当前环节的内容
	{
		// 当前事件id：
		try {
			System.out.println("查询当前环节的内容：");

			currentContents = psContentService.findContents(getCurrentPsId());
			System.out.println("当前环节内容：CurrentPsContent:  "
					+ currentContents.size());
			message = "findContents";

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "currentContents";
	}

	public String findStepLog() // 查询当前步骤的要更新的日志,最主要的方法
	{
		try {
			String psId = this.getCurrentPsId();
			System.out.println("定期当前的步骤环节id " + psId);
			if (this.getCurrentPsId() != null) {

				List<TbLog> list = logService.find(psId, maxTime);

				if (list.size() >= 1) {
					System.out
							.println(list.get(0).getLogContent()
									+ "**getLogContent***************************getLogTime******"
									+ list.get(0).getLogTime());
					logList = list;
					System.out.println(" 查出来的日志： " + logList);
				} else {
					logList = null;
				}
			} else {
				System.out
						.println("**********************没有拿到当前环节ID，可能该预案没有环节******************************************************");
			}
			// if (logList != null) {
			// System.out.println(" 查出来的日志的条数： " + logList.size());
			// int index = logList.size() - 1;
			// if (index >= 0)
			// newlogid = logList.get(index).getLogId();
			// }
			//
			// System.out.println("查询后的日志的id的最大值：" + newlogid);
			// message = newlogid;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "findLogs";
	}

	public String getLogInAnalyse() {

		if (eventId != null) {
			if (logService.findInAnalyse(eventId, maxTime).size() != 0) {
				logList = logService.findInAnalyse(eventId, maxTime);
			}else{
				logList = null;
			}
		} else {
			System.out
					.println("********************事件ID为空**************************************");
		}

		return "findLogs";

	}

	public String findLogType() // 查询日志类型
	{
		try {
			System.out.println("万才的action： 查询日志类型");
			logTypeList = logTypeService.findAll();
			message = "find_LogType";

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findLogTypes";
	}

	public String endPlanStep() // 预案启动结束
	{
		this.newlogid = "10000000000000000000000000000000";
		System.out.println("action: 预案启动结束  newLogId: " + newlogid);

		message = "endPlanStep";
		return "endPlanStep";
	}

	public String getPsId() {
		return psId;
	}

	public TbCurrentEventDAO getCurrentEventDAO() {
		return currentEventDAO;
	}

	public void setCurrentEventDAO(TbCurrentEventDAO currentEventDAO) {
		this.currentEventDAO = currentEventDAO;
	}

	public List<TbLog> getLogList() {
		return logList;
	}

	public void setCurrentContents(List<TbPsContent> currentContents) {
		this.currentContents = currentContents;
	}

	public void setPsId(String psId) {
		this.psId = psId;
	}

	public ILogPlanStepService getService() {
		return service;
	}

	public void setService(ILogPlanStepService service) {
		this.service = service;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public TbLogTypeService getLogTypeService() {
		return logTypeService;
	}

	public void setLogTypeService(TbLogTypeService logTypeService) {
		this.logTypeService = logTypeService;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public TbLogPsContentService getPsContentService() {
		return psContentService;
	}

	public void setPsContentService(TbLogPsContentService psContentService) {
		this.psContentService = psContentService;
	}

	public List<TbPsContent> getCurrentContents() {
		return currentContents;
	}

	public void setCurrentContent(List<TbPsContent> currentContents) {
		this.currentContents = currentContents;
	}

	public String getAfterLogId() {
		return afterLogId;
	}

	public void setAfterLogId(String afterLogId) {
		this.afterLogId = afterLogId;
	}

	public List<TbLogType> getLogTypeList() {
		return logTypeList;
	}

	public void setLogTypeList(List<TbLogType> logTypeList) {
		this.logTypeList = logTypeList;
	}

}
